# -*- coding: utf-8 -*-
"""
Created on 2018年10月22日
@author: Administrator
"""
# 1.最常规的匹配
# re模块有一个match函数
import re

content = 'Hello 123 4567 World_This is a Regex Demo'
print(len(content))
result = re.match('^Hello\s\d\d\d\s\d{4}\s\w{10}.*Demo$', content)
# \d{4}表示4个\d
print(result)
print(result.group())  # 显示匹配的结果
print(result.span())  # 显示字符串的长度范围

# 2.泛匹配
import re
content = 'Hello 123 4567 World_This is a Regex Demo'
result = re.match('^Hello.*Demo$', content)
print(result)
print(result.group())  # 显示匹配的结果
print(result.span())  # 显示字符串的长度范围

# 3.匹配目标
import re
content = 'Hello 1234567 World_This is a Regex Demo'
result = re.match('^Hello\s(\d+)\sWorld.*Demo$', content)
print(result)
print(result.group())  # 显示匹配的结果
print(result.span())  # 显示字符串的长度范围

# 4.贪婪匹配
import re
content = 'Hello 1234567 World_This is a Regex Demo'
result = re.match('^He.*(\d+).*Demo$', content)
print(result)
print(result.group(1))

# 5.非贪婪匹配
import re
content = 'Hello 1234567 World_This is a Regex Demo'
result = re.match('^He.*?(\d+).*Demo$', content)
print(result)
print(result.group(1))

# 6.匹配模式
import re
content = '''Hello 1234567 World_This
is a Regex Demo
'''
result = re.match('^He.*?(\d+).*?Demo$', content, re.S)
print(result)

# 7(1).无转义，结果为None
import re
content = 'price is $5.00'
result = re.match('price is $5.00', content)
print(result)
# 7(2).有转义，结果不为None
import re
content = 'price is $5.00'
result = re.match('price is \$5\.00', content)
print(result)
# 7(3).去掉p，结果为None
import re
content = 'price is $5.00'
result = re.match('rice is \$5\.00', content)
print(result)
# 总结：尽量使用泛匹配、使用括号得到匹配目标、尽量使用非贪婪模式、有换行符就用re.S
#       使用re.match是从第一个字符进行匹配的，如果去掉第一个字符就匹配不到结果了，比如将7(2)中p去掉，如7(3),结果就为None
